package com.example.xingzai.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ThirdActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        expandableListView = (ExpandableListView) findViewById(R.id.expandablelistview);
        expandableListView.setAdapter(new MyAdapter());
    }


    private class MyAdapter extends BaseExpandableListAdapter {

        private String[] group = new String[]{"第一组", "第二组", "第三组"};
        private String[][] child = new String[][]{{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9", "0"}};

        //父项的数量
        @Override
        public int getGroupCount() {
            return group.length;
        }

        //父项中子项的数量
        @Override
        public int getChildrenCount(int groupPosition) {
            return child[groupPosition].length;
        }

        //获得某个父项
        @Override
        public Object getGroup(int groupPosition) {
            return group[groupPosition];
        }

        //获得父项中的某个子项
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return child[groupPosition][childPosition];
        }

        //某个父项的id
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //父项中某个子项的id
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_01, null);
            }
            TextView textView01 = (TextView) convertView.findViewById(R.id.text_group);
            textView01.setText(group[groupPosition]);

            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_02, null);
            }
            TextView textView02 = (TextView) convertView.findViewById(R.id.text_child);
            textView02.setText(child[groupPosition][childPosition]);
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}
